home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 351-375 / disk_356 / algorhythms / source / musicserial.c < prev    next >
C/C++ Source or Header  |  1992-05-06  |  6KB  |  236 lines

  1. /* musicserial.c */
  2. /* Copyright 1990 Thomas E. Janzen All Rights Reserved */
  3. /*
  4. **  FACILITY:
  5. **
  6. **    AlgoRhythms music improviser on Commodore (TM) Amiga (TM)
  7. **    compiled with Lattice (TM) C 5.05
  8. **
  9. **  ABSTRACT:
  10. **
  11. **    Algorhythms improvises music over the MIDI serial port.
  12. **
  13. **  AUTHORS: Thomas E. Janzen
  14. **
  15. **  CREATION DATE:    26-MAR-1990
  16. **
  17. **  MODIFICATION HISTORY:
  18. **    DATE    NAME    DESCRIPTION
  19. **--
  20. */
  21.  
  22. #include    "exec/types.h"
  23. #include    "exec/nodes.h"
  24. #include    "exec/lists.h"
  25. #include    "exec/ports.h"
  26. #include    "exec/libraries.h"
  27. #include    "exec/devices.h"
  28. #include    "devices/serial.h"
  29. #include    "exec/io.h"
  30. #include    "intuition/intuition.h"
  31. #include <proto/dos.h>
  32. #include <proto/graphics.h>
  33. #include <proto/exec.h>
  34. #include <proto/mathffp.h>
  35. #include <proto/intuition.h>
  36.  
  37. #define NOTEON (0x90)
  38. #define NOTEOFF (0x80)
  39. #define MIDIADDRESS (0x00)
  40. #define NOTEONCMD (0x90)
  41. #define NOTEOFFCMD (0x80)
  42. #define STARTFUNCT (0)
  43. #define STOPFUNCT (1)
  44. #define CLOCKFUNCT (2)
  45. #define CONTFUNCT (3)
  46. #define START 0xFA
  47. #define STOP 0xFC
  48. #define CONTINUE 0xFB
  49. #define TIMINGCLOCK 0xF8
  50.  
  51. extern struct Window *w;
  52. extern int fubar;
  53.  
  54. int midi_addr=0;
  55. extern int quit;
  56. int Response;
  57.  
  58. #include "AlgoRhythms.h"
  59.  
  60. extern struct GfxBase *GfxBase;
  61. extern struct IntuitionBase *IntuitionBase;
  62. extern struct DOSBase *DOSBase;
  63. extern struct MathBase *MathBase;
  64.  
  65. struct IOExtSer *IORser;
  66. struct MsgPort *port;
  67.  
  68. extern struct IORequest *CreateExtIO();
  69.  
  70. /* Devices prototypes */
  71.  
  72. extern void DeletePort(struct MsgPort *);
  73.  
  74. const char QuitStr[]="Quit";
  75. const char SerErrStr[]="Serial Device Error";
  76. const char PrtErrStr[]="Port Error";
  77. const char IOErrStr[]="Create ExtIO Error";
  78. const char DevErrStr[]="Serial Device Error";
  79. const char PrmErrStr[]="Set Parm Error";
  80. const char WrtErrStr[]="Serial.device write error";
  81.  
  82. struct IntuiText QuitTxt={2,1,JAM1,5,4,NULL,QuitStr,NULL};
  83.  
  84. void SendFunction(int Function);
  85.  
  86. char    playbuffer[4];    /* MIDI note message buffer     */
  87.  
  88. int SetParams(struct IOExtSer *io,unsigned long rbuf_len,
  89.     unsigned char rlen,unsigned char wlen,unsigned long brk,
  90.     unsigned long baud,unsigned char sf,
  91.     unsigned long ta0,unsigned long ta1)
  92. {
  93.     struct IntuiText SerErrTxt={2,1,JAM1,5,15,NULL,SerErrStr,NULL};
  94.     int error;
  95.     io->io_ReadLen    =rlen;
  96.     io->io_BrkTime    =brk; /*length of break timej (irrelevant)*/
  97.     io->io_Baud    =baud;
  98.     io->io_WriteLen    =wlen;
  99.     io->io_StopBits    =0x01;
  100.     io->io_RBufLen    =rbuf_len;
  101.     io->io_SerFlags    =(1<<SERB_RAD_BOOGIE);
  102.     io->IOSer.io_Command=SDCMD_SETPARAMS;
  103.     io->io_TermArray.TermArray0=ta0;
  104.     io->io_TermArray.TermArray1=ta1;
  105.     if((error=DoIO(io)) !=0) {
  106.         quit=TRUE;
  107.         fubar=TRUE;
  108.         Response=AutoRequest(w,&SerErrTxt,
  109.             &QuitTxt,&QuitTxt,0L,0L,300L,60L);
  110.     }
  111.     return(error);
  112. }
  113.  
  114. void Open_MIDI_Port(void)
  115. {
  116.     struct IntuiText PrtErrTxt={2,1,JAM1,5,15,NULL,PrtErrStr,NULL};
  117.     struct IntuiText IOErrTxt={2,1,JAM1,5,15,NULL,IOErrStr,NULL};
  118.     struct IntuiText DevErrTxt={2,1,JAM1,5,15,NULL,DevErrStr,NULL};
  119.     struct IntuiText PrmErrTxt={2,1,JAM1,5,15,NULL,PrmErrStr,NULL};
  120.  
  121.     int error;
  122.     unsigned long rbl = 512;    /*read buffer length*/
  123.     unsigned long brk = 750000; 
  124.         /* length of break in usec, usu. 750000 */
  125.     unsigned long baud= 31250;    /*baud rate 31.25k*/
  126.     unsigned long t0  = 0x51040303;    /*termination characters*/
  127.     unsigned long t1  = 0x03030303;
  128.     unsigned char rwl = 0x08; /*bits per read char */
  129.     unsigned char wwl = 0x08;/*bits per write char */
  130.     unsigned char sf  = 0x00; /*serial flags cf D-124 in libs devices*/
  131.     /* Try to get to the serial device. */
  132.     /* page 396 of Libraries and Devices was the example*/
  133.  
  134.     port=CreatePort(SERIALNAME,0);
  135.     if(port==NULL){
  136.         quit=TRUE;
  137.         fubar=TRUE;
  138.         Response=AutoRequest(w,&PrtErrTxt,
  139.             &QuitTxt,&QuitTxt,0L,0L,300L,60L);
  140.     }
  141.     IORser= 
  142.     (struct IOExtSer *)CreateExtIO(port,sizeof(struct IOExtSer));
  143.     if(IORser == NULL) {
  144.         quit=TRUE;
  145.         fubar=TRUE;
  146.         Response=AutoRequest(w,&IOErrTxt,
  147.             &QuitTxt,&QuitTxt,0L,0L,300L,60L);
  148.     }
  149.     open:
  150.     if ((error = OpenDevice(SERIALNAME,0,IORser,0))!=0) {
  151.         quit=TRUE;
  152.         fubar=TRUE;
  153.         Response=AutoRequest(w,&DevErrTxt,
  154.             &QuitTxt,&QuitTxt,0L,0L,300L,60L);
  155.     }
  156.  
  157.     if((error=SetParams(IORser,rbl,rwl,wwl,brk,baud,sf,t0,t1))!=0){
  158.         fubar=TRUE;
  159.         quit=TRUE;
  160.         Response=AutoRequest(w,&PrmErrTxt,
  161.             &QuitTxt,&QuitTxt,0L,0L,300L,60L);
  162.         }
  163.     /* The serial device is open, so go ahead and play music.*/
  164. }
  165.  
  166. int WriteSer(struct IOExtSer *io,char *data,int length)
  167. {
  168.     int    error;
  169.     struct IntuiText WrtErrTxt={2,1,JAM1,5,15,NULL,WrtErrStr,NULL};
  170.  
  171.     io->IOSer.io_Data=(APTR)data;
  172.     io->IOSer.io_Length=length;
  173.     io->IOSer.io_Command=CMD_WRITE;
  174.  
  175.     if((error=DoIO(io))!=0){
  176.         quit=TRUE;
  177.         fubar=TRUE;
  178.         Response=AutoRequest(w,&WrtErrTxt,
  179.             &QuitTxt,&QuitTxt,0L,0L,300L,60L);
  180.     }
  181.     return(error);
  182. }
  183.  
  184. void PlayNoteOn(const struct NoteEvent *PlayEvent) { 
  185.     extern int scale[];
  186.     /* Play a note; give note, dynamic*/
  187.     playbuffer[0]=(unsigned char)(PlayEvent->Channel | NOTEONCMD);
  188.     playbuffer[1]=(unsigned char)(scale[PlayEvent->Pitch]);
  189.     playbuffer[2]=(unsigned char)(PlayEvent->Dynamic);
  190.     WriteSer(IORser,playbuffer,3);    /* send it out MIDI */
  191. }
  192.  
  193. void PlayNoteOff(const struct NoteEvent *StopEvent) {    
  194.     /* MIDI note off routine ; input is number of note */ 
  195.     extern int    scale[];
  196.     playbuffer[0]=(unsigned char)(StopEvent->Channel | NOTEOFFCMD);
  197.     playbuffer[1]=(unsigned char)(StopEvent->CurPitch); 
  198.         /* note number */
  199.     playbuffer[2]=0;        /* zero dynamic */
  200.     WriteSer(IORser,playbuffer,3); /*send it out MIDI */
  201. }
  202.  
  203. void SendFunction(int Function) { 
  204.     playbuffer[1]=0;
  205.     switch (Function) {
  206.         case STARTFUNCT:
  207.         playbuffer[0]=(unsigned char)(START);
  208.         break;
  209.         case STOPFUNCT:
  210.         playbuffer[0]=(unsigned char)(STOP);
  211.         break;
  212.         case CLOCKFUNCT:
  213.         playbuffer[0]=(unsigned char)(TIMINGCLOCK);
  214.         break;
  215.         case CONTFUNCT:
  216.         playbuffer[0]=(unsigned char)(CONTINUE);
  217.         break;
  218.     }
  219.     WriteSer(IORser,playbuffer,1);    /* send it out MIDI */
  220. }
  221.  
  222. void StopAllNotes(const struct NoteEvent NotestoStop[]) {
  223.     int i;
  224.     for(i=0;i<16;i++) {
  225.         PlayNoteOff(&NotestoStop[i]);
  226.         if(fubar == TRUE) break;
  227.     }
  228.     SendFunction(STOPFUNCT);
  229. }
  230.  
  231. void StopMIDI(void){
  232.     if (port) DeletePort(port);
  233.     if (IORser) CloseDevice(IORser);
  234. }
  235.  
  236.